package kr.saeildamil.androidforstudy2.ch25;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

class TextPreference {
	String mPath;
	StringBuilder mBuff;
	static final String HEADER = "_ _Text Preference File_ _\n";

	public TextPreference(String Path) throws Exception {
		mPath = Path;
		File file = new File(mPath);
		if (file.exists() == false) {
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(HEADER.getBytes());
			fos.close();
		}
	}

	public void Reset() {
		File file = new File(mPath);
		file.delete();
	}

	public boolean Ready() {
		FileInputStream fis;
		try {
			fis = new FileInputStream(mPath);
			int available = fis.available();
			byte[] data = new byte[available];
			fis.read();
			fis.close();
			mBuff = new StringBuilder(available);
			mBuff.append(new String(data));
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	public boolean CommitWrite() {
		File file = new File(mPath);
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(file);
			fos.write(mBuff.toString().getBytes());
			fos.close();
		} catch (Exception e) {
			return false;
		}
		mBuff = null;
		return true;
	}

	public void EndReady() {
		mBuff = null;
	}

	int FindIdx(String name) {
		String key = "_ _" + name + "=";
		int idx = mBuff.indexOf(key);
		if (idx == -1) {
			return -1;
		} else {
			return idx + key.length();
		}
	}

	public void WriteString(String name, String value) {
		int idx = FindIdx(name);
		if (idx == -1) {
			mBuff.append("_ _");
			mBuff.append(name);
			mBuff.append("=");
			mBuff.append(value);
			mBuff.append("\n");
		} else {
			int end = mBuff.indexOf("\n", idx);
			mBuff.delete(idx, end);
			mBuff.insert(idx, value);
		}
	}

	public String readString(String name, String def) {
		int idx = FindIdx(name);
		if (idx == -1) {
			return def;
		} else {
			int end = mBuff.indexOf("\n", idx);
			return mBuff.substring(idx, end);
		}
	}

	public void WriteInt(String name, int value) {
		WriteString(name, Integer.toString(value));
	}

	public int ReadInt(String name, int def) {
		String s = readString(name, "_ _none");
		if (s.equals("_ _none")) {
			return def;
		}
		try {
			return Integer.parseInt(s);
		} catch (Exception e) {
			return def;
		}
	}

	public void WriteLong(String name, long value) {
		WriteString(name, Long.toString(value));
	}

	public long ReadLong(String name, long def) {
		String s = readString(name, "_ _none");
		if (s.equals("__none")) {
			return def;
		}
		try {
			return Long.parseLong(s);
		} catch (Exception e) {
			return def;
		}
	}

	public void WriteBoolean(String name, boolean value){
		WriteString(name, value ? "1":"0");
	}
	
	public boolean ReadBoolean(String name, boolean def){
		String s = readString(name, "_ _none");
		if(s.equals("_ _none")){
			return def;
		}try{
			return s.equals("1") ? true:false;
		}catch(Exception e){
			return def;
		}
	}
	
	public void WriteFloat(String name, float value){
		WriteString(name, Float.toString(value));
	}
	
	public float ReadFloat(String name, float def){
		String s = readString(name, "_ _none");
		if(s.equals("_ _none")){
			return def;
		}try{
			return Float.parseFloat(s);
		}catch(Exception e){
			return def;
		}
	}
	
	void BulkWriteReady(int length){
		mBuff = new StringBuilder(length);
		mBuff.append(HEADER);
		mBuff.append("\n");
	}
	
	void BulkWrite(String name, String value){
		mBuff.append("_ _");
		mBuff.append(name);
		mBuff.append("=");
		mBuff.append(value);
		mBuff.append("\n");
	}
	
	void DeleteKey(String name){
		int idx = FindIdx(name);
		if(idx != -1){
			int end = mBuff.indexOf("\n", idx);
			mBuff.delete(idx - (name.length() + 3), end + 1);
		}
	}
}
